<?php

namespace app\common\model\user;

use app\common\model\CommonBaseModel;
use app\common\utils\MysqlUtils;

class User extends CommonBaseModel
{
    /*
    CREATE TABLE `users` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
      `username` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
      `mobile` varchar(50) NOT NULL DEFAULT '' COMMENT '手机号',
      `password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码',
      `nickname` varchar(50) NOT NULL DEFAULT '' COMMENT '昵称',
      `avatar` varchar(512) NOT NULL DEFAULT '' COMMENT '头像URL',
      `last_login_time` datetime DEFAULT NULL COMMENT '最后一次登录时间',
      `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态:1正常,2禁用',
      `is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除:0未删除,1已删除',
      `add_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
      `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
    */

    protected $table = 'users';

    //status 状态:1正常,2禁用
    const STATUS_NORMAL = 1;
    const STATUS_DISABLED = 2;
    const STATUS_MAP = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_DISABLED => '禁用',
    ];

    //直接导出数据的最大条数
    const MAX_EXPORT_COUNT = 500;

    public static function getCountAndList($params): array
    {
        $where_rule = [
            'username' => ['=', ['username']],
            'nickname' => ['like', ['nickname']],
            'id' => ['in', ['id']],
            'add_time' => ['between', ['add_time_start', 'add_time_end']],
        ];
        $where = MysqlUtils::getWhere($where_rule, $params);
        $count = parent::getDataCount($where);
        $list = parent::getDataList($where, 'id', 'asc', $params['page'] ?? null, $params['page_size'] ?? null);

        return [$count, $list];
    }
}